﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.MountAndBlade.View.Tableaus.SceneTableau
// Assembly: TaleWorlds.MountAndBlade.View, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 61152EB8-513B-4D4A-8AC0-0D1465C6CF6C
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\Modules\Native\bin\Win64_Shipping_Client\TaleWorlds.MountAndBlade.View.dll

using TaleWorlds.Core;
using TaleWorlds.DotNet;
using TaleWorlds.Engine;
using TaleWorlds.Engine.Options;
using TaleWorlds.Library;

#nullable disable
namespace TaleWorlds.MountAndBlade.View.Tableaus
{
  public class SceneTableau
  {
    private float _animationFrequencyThreshold = 2.5f;
    private MatrixFrame _frame;
    private Scene _tableauScene;
    private Camera _continuousRenderCamera;
    private GameEntity _cameraEntity;
    private float _cameraRatio;
    private bool _initialized;
    private int _tableauSizeX;
    private int _tableauSizeY;
    private SceneView View;
    private bool _isRotatingCharacter;
    private float _animationGap;
    private bool _isEnabled;
    private float RenderScale = 1f;

    public Texture _texture { get; private set; }

    public bool? IsReady => this.View?.ReadyToRender();

    public SceneTableau() => this.SetEnabled(true);

    private void SetEnabled(bool enabled)
    {
      this._isEnabled = enabled;
      this.View?.SetEnable(this._isEnabled);
    }

    private void CreateTexture()
    {
      this._texture = Texture.CreateRenderTarget(nameof (SceneTableau), this._tableauSizeX, this._tableauSizeY, true, false);
      this.View = SceneView.CreateSceneView();
      this.View.SetScene(this._tableauScene);
      this.View.SetRenderTarget(this._texture);
      this.View.SetAutoDepthTargetCreation(true);
      this.View.SetSceneUsesSkybox(true);
      this.View.SetClearColor(4294902015U);
    }

    public void SetTargetSize(int width, int height)
    {
      this._isRotatingCharacter = false;
      if (width <= 0 || height <= 0)
      {
        this._tableauSizeX = 10;
        this._tableauSizeY = 10;
      }
      else
      {
        this.RenderScale = NativeOptions.GetConfig(NativeOptions.NativeOptionsType.ResolutionScale) / 100f;
        this._tableauSizeX = (int) ((double) width * (double) this.RenderScale);
        this._tableauSizeY = (int) ((double) height * (double) this.RenderScale);
      }
      this._cameraRatio = (float) this._tableauSizeX / (float) this._tableauSizeY;
      SceneView view = this.View;
      this.View?.SetEnable(false);
      this.View?.AddClearTask(true);
      this.CreateTexture();
    }

    public void OnFinalize()
    {
      if ((NativeObject) this._continuousRenderCamera != (NativeObject) null)
      {
        this._continuousRenderCamera.ReleaseCameraEntity();
        this._continuousRenderCamera = (Camera) null;
        this._cameraEntity = (GameEntity) null;
      }
      this.View?.SetEnable(false);
      this.View?.AddClearTask();
      this._texture.ReleaseNextFrame();
      this._texture = (Texture) null;
      this._tableauScene = (Scene) null;
    }

    public void SetScene(object scene)
    {
      if (scene is Scene scene1)
      {
        this._tableauScene = scene1;
        if (this._tableauSizeX == 0 || this._tableauSizeY == 0)
          return;
        this.CreateTexture();
      }
      else
        Debug.FailedAssert("Given scene object is not Scene type", "C:\\Develop\\MB3\\Source\\Bannerlord\\TaleWorlds.MountAndBlade.View\\Tableaus\\SceneTableau.cs", nameof (SetScene), 120);
    }

    public void SetBannerCode(string value) => this.RefreshCharacterTableau();

    private void RefreshCharacterTableau(Equipment oldEquipment = null)
    {
      if (this._initialized)
        return;
      this.FirstTimeInit();
    }

    public void RotateCharacter(bool value) => this._isRotatingCharacter = value;

    public void OnTick(float dt)
    {
      if ((double) this._animationFrequencyThreshold > (double) this._animationGap)
        this._animationGap += dt;
      if (!((NativeObject) this.View != (NativeObject) null))
        return;
      if ((NativeObject) this._continuousRenderCamera == (NativeObject) null)
      {
        GameEntity entityWithTag = this._tableauScene.FindEntityWithTag("customcamera");
        if ((NativeObject) entityWithTag != (NativeObject) null)
        {
          this._continuousRenderCamera = Camera.CreateCamera();
          Vec3 dofParams = new Vec3();
          entityWithTag.GetCameraParamsFromCameraScript(this._continuousRenderCamera, ref dofParams);
          this._cameraEntity = entityWithTag;
        }
      }
      this.PopupSceneContinuousRenderFunction();
    }

    private void FirstTimeInit() => this._initialized = true;

    private void PopupSceneContinuousRenderFunction()
    {
      GameEntity entityWithTag = this._tableauScene.FindEntityWithTag("customcamera");
      this._tableauScene.SetShadow(true);
      this._tableauScene.EnsurePostfxSystem();
      this._tableauScene.SetMotionBlurMode(true);
      this._tableauScene.SetBloom(true);
      this._tableauScene.SetDynamicShadowmapCascadesRadiusMultiplier(1f);
      this.View.SetRenderWithPostfx(true);
      this.View.SetSceneUsesShadows(true);
      this.View.SetScene(this._tableauScene);
      this.View.SetSceneUsesSkybox(true);
      this.View.SetClearColor(4278190080U);
      this.View.SetFocusedShadowmap(false, ref this._frame.origin, 1.55f);
      this.View.SetEnable(true);
      if (!((NativeObject) entityWithTag != (NativeObject) null))
        return;
      Vec3 dofParams = new Vec3();
      entityWithTag.GetCameraParamsFromCameraScript(this._continuousRenderCamera, ref dofParams);
      if (!((NativeObject) this._continuousRenderCamera != (NativeObject) null))
        return;
      this.View.SetCamera(this._continuousRenderCamera);
    }
  }
}
